#include "create.h"
#include "ui_create.h"
#include "tomatoclock.h"
#include "databasemanager.h"
#include <QDebug>
#include "stop.h"
#include "ui_stop.h"

extern Stop *st;
Create::Create(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Create)
{
    ui->setupUi(this);
   ui->frame->setStyleSheet("background-color: white;");
}

Create::~Create()
{
    //delete ui;
}

void Create::on_pushButton_clicked()
{    Stop *s=new Stop;
    s->show();
    tomatoclock *t1=new tomatoclock;
    t1->close();
    QString s1=ui->project->text();
    QString hour=ui->lineEdit->text();
    QString minute=ui->lineEdit_2->text();
    QString second=ui->lineEdit_3->text();
    QString s2=QString("%1,:,%2,:,%3").arg(hour).arg(minute).arg(second);
    QString s3=ui->project_2->text();
    //获得duration
    QStringList parts = s2.split(',', Qt::SkipEmptyParts);
    while (parts.size() < 3) {
        parts.append("0");
    }

    bool okHour, okMinute, okSecond;
    int hour_ = parts[0].toInt(&okHour);
    int minute_ = parts[1].toInt(&okMinute);
    int second_ = parts[2].toInt(&okSecond);
    // 如果任何部分的转换失败，设置默认值为0
    if (!okHour) hour_ = 0;
    if (!okMinute) minute_ = 0;
    if (!okSecond) second_ = 0;
    int totalMinutes = hour_ * 60 + minute_ + second_ / 60; // 注意：秒数转为分钟需除以60
    int length = totalMinutes;
    if (length == 0) {
        length = 1; // 小于一分钟计为一分钟
    }
    DatabaseManager::insert_history(s1,s3,length);

    st->setorigin(length);
    qDebug()<<"创建时计入stop的length="<<length;
    st->ui->project_1->setText(s1);
    st->m_time=hour.toDouble()*360000+minute.toDouble()*6000+second.toDouble()*100;
    st->cm_time=hour.toDouble()*360000+minute.toDouble()*6000+second.toDouble()*100;
    st->ui->label->setTime(st->cm_time-100);
    emit itemDataChanged();
    this->close();

}//创建成功返回


void Create::on_pushButton_2_clicked()
{
    this->close();
}//创建失败，返回


